<?php 
	class Tasks extends CI_Model{

		function __construct(){
			parent::__construct();
		}

		function get_task_list($limit = 0, $offset = 10, $conditions = array()){
			$today_time = strtotime(date("Y-m-d", time()));
			$week = date('N', time());
			$where = " WHERE 1 = 1 ";
			if(!empty($conditions['type'])){
				$where .= " AND type.type IN({$conditions['type']}) ";
			}
			if(!empty($conditions['pid'])){
				$where .= " AND type.pid IN({$conditions['pid']}) ";
			}
			if(!empty($conditions['child_id'])){
				$where .= " AND task.child_id='{$conditions['child_id']}'";
			}
			if(!empty($conditions['find_ini_set'])){
				$where .= " AND find_in_set($week, task.repeat) ";
			}
			$sql = "SELECT SQL_CALC_FOUND_ROWS task.*, type.type, type.image, tr.id AS record_id, tr.stars AS has_stars,  type.pid, type.default_time,type.remark FROM {$this->db->dbprefix}tasks AS task ";
			$sql .= "LEFT JOIN {$this->db->dbprefix}task_types AS type ON(type.id=task.task_type_id) ";
			$sql .= "LEFT JOIN {$this->db->dbprefix}task_records AS tr ON(tr.task_id=task.id AND tr.time >= $today_time) ";
			$sql .= "$where ORDER BY task.remind_hour ASC, task.remind_min ASC";
			if($limit > 0 || $offset > 0){
				$sql .= " LIMIT $limit, $offset";
			}
			$result = $this->db->query($sql)->result_array();
			$row_num =  $this->db->query('SELECT FOUND_ROWS() as row_num;')->row_array();
			return array("count" => $row_num['row_num'], "list" => $result);
		}

		function get_reg_push_list(){
			$sql = "SELECT * FROM {$this->db->dbprefix}task_types WHERE reg_push = 1 AND status = 1";
			$result = $this->db->query($sql)->result_array();
			return $result;
		}
	}